﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;
using System.Configuration;

namespace MonitorFileConvert
{
    public partial class GetImage : Form
    {
        ThreadClass s;    //声明一个线程实体

        public GetImage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取文件扩展名列表
        /// </summary>
        /// <returns></returns>
        private List<string> GetFileExtends()
        {
            List<string> list = new List<string>();
            if (this.checkFlv.Checked)
                list.Add(".flv");
            if (this.checkMp4.Checked)
                list.Add(".mp4");
            if (this.checkWmv.Checked)
                list.Add(".wmv");
            if (this.checkMpeg.Checked)
                list.Add(".mpeg");
            if (this.checkAvi.Checked)
                list.Add(".avi");
            if (this.check3GP.Checked)
                list.Add(".3gp");
            if (this.checkMpg.Checked)
                list.Add(".mpg");
            return list;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(this.textFilesPath.Text.Trim()))
            //{
            //    Random random = new Random(System.Environment.TickCount);
            //    Execute(this.textFilesPath.Text.Trim(), random, GetFileExtends());
            //}
            this.labelCounts.Text = "0";
            this.button1.Enabled = false;
            if (!string.IsNullOrEmpty(this.textFilesPath.Text.Trim()))
            {
                Random random = new Random(System.Environment.TickCount);
                //初始化线程类
                s = new ThreadClass(this, this.textFilesPath.Text.Trim(), random, GetFileExtends());
                //订阅事件
                s.BeforeHandle += new ThreadClass.delegateBefore(Before);
                s.ExcutingHandle += new ThreadClass.delegateExcuting(Execute);
                s.ErrorHandle += new ThreadClass.delegateError(Error);
                s.CompleteHandle += new ThreadClass.delegateComplete(Complete);
                //运行线程
                s.Start();
            }
            else
            {
                this.textMemo.Text = "文件路径不能为空！";
                this.button1.Enabled = true;
            }

        }

        #region  同步方法

        public void Execute(string directoryPath, Random random, List<string> list)
        {
            System.IO.DirectoryInfo direct = new DirectoryInfo(directoryPath);
            foreach (System.IO.DirectoryInfo dir in direct.GetDirectories())
            {
                Execute(dir.FullName, random, list);
            }
            foreach (System.IO.FileInfo file in direct.GetFiles())
            {

                CreateImageFromFile(file, random, list);
            }
        }


        public void CreateImageFromFile(System.IO.FileInfo file, Random random, List<string> list)
        {
            try
            {
                if (list.Contains(file.Extension.Trim().ToLower()))
                {
                    this.textMemo.Text += file.FullName + "\r\n";

                    string strImage = file.Name.Replace(file.Extension, string.Empty);

                    string strFfmpegPath = Application.StartupPath + (@"\ffmpeg.exe");
                    string  ss = GetRandom(random);
                    //ffmpeg.exe -i 19_11574.mp4 -y  -f image2 -ss 3  -s 160*120 tt1.jpg

                    #region 生成图片
                    //小图
                    if (this.checkSmall.Checked)
                    {
                        string[] strSmallSize = this.textSmall.Text.Trim().Split('*');
                        string strArguments_small = "-i " + file.FullName + " -y  -ss " + ss + "  -s " + strSmallSize[0].ToString() + "*" + strSmallSize[1].ToString() + " -f image2 " + file.FullName.Replace(file.Extension, "_small.jpg");
                        Tools.CreateProcess(strFfmpegPath, strArguments_small, false);
                    }

                    //中图
                    if (this.checkMiddle.Checked)
                    {
                        string[] strMiddleSize = this.textMiddle.Text.Trim().Split('*');
                        string strArguments_middle = "-i " + file.FullName + " -y  -ss " + ss + "  -s " + strMiddleSize[0].ToString() + "*" + strMiddleSize[1].ToString() + " -f image2 " + file.FullName.Replace(file.Extension, "_middle.jpg");
                        Tools.CreateProcess(strFfmpegPath, strArguments_middle, false);
                    }

                    //大图
                    if (this.checkBig.Checked)
                    {
                        string[] strBigSize = this.textBig.Text.Trim().Split('*');
                        string strArguments_big = "-i " + file.FullName + " -y  -ss " + ss + "  -s " + strBigSize[0].ToString() + "*" + strBigSize[1].ToString() + " -f image2 " + file.FullName.Replace(file.Extension, "_big.jpg");
                        Tools.CreateProcess(strFfmpegPath, strArguments_big, false);
                    }
                    #endregion

                }
            }
            catch (Exception ex)
            {
                this.textMemo.Text += file.FullName + ":error-" + ex.Message + "\r\n";
            }

        }

        private string GetRandom(Random random)
        {
            //Random random = new Random(System.Environment.TickCount);
            return random.Next(1, 60).ToString();
        }

        #endregion


        # region 大中小图的事件
        private void checkSmall_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)sender).Checked)
            {
                if (string.IsNullOrEmpty(this.textSmall.Text.Trim()))
                    this.textSmall.Text = "120*90";
            }
            else
            {
                this.textSmall.Text = string.Empty;
            }
        }

        private void checkMiddle_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)sender).Checked)
            {
                if (string.IsNullOrEmpty(this.textMiddle.Text.Trim()))
                    this.textMiddle.Text = "350*280";
            }
            else
            {
                this.textMiddle.Text = string.Empty;
            }
        }

        private void checkBig_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)sender).Checked)
            {
                if (string.IsNullOrEmpty(this.textBig.Text.Trim()))
                    this.textBig.Text = "600*480";
            }
            else
            {
                this.textBig.Text = string.Empty;
            }
        }

        #endregion


        public void Before(TEventArgs e)
        {
            this.textMemo.Text += e.stringFileFullPath + "\r\n";
            this.textMemo.SelectionStart = this.textMemo.Text.Length;
            this.textMemo.ScrollToCaret();
        }

        public void Execute(TEventArgs e)
        {
            this.labelCounts.Text = (Convert.ToInt32(this.labelCounts.Text) + e.intMovisFilesCount).ToString();
            this.textMemo.SelectionStart = this.textMemo.Text.Length;
            this.textMemo.ScrollToCaret();
        }

        public void Error(TEventArgs e)
        {
            this.textMemo.Text += e.stringError + "\r\n";
            this.textMemo.SelectionStart = this.textMemo.Text.Length;
            this.textMemo.ScrollToCaret();
        }


        public void Complete(TEventArgs e)
        {
            this.textMemo.Text += "已完成此次操作\r\n";
            this.textMemo.SelectionStart = this.textMemo.Text.Length;
            this.textMemo.ScrollToCaret();
            this.button1.Enabled = true;
        }

        private void GetImage_FormClosed(object sender, FormClosedEventArgs e)
        {
            if(null != s)
                s.End();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new GetSwf().Show();
        }


    }
}
